#include <stdio.h>
#include "typecheck_verify.h"

void typecheck_verify(BODY *root, SymbolTable *symboltable) {
	typecheck_STATEMENTLIST(root->statement_list, symboltable);
}

// Not tested yet
int typecheckCOMPARE(TYPE *type1, TYPE *type2) {
	if (type1->kind == type2->kind) {
		switch (type1->kind) {
		case type_idK:
			printf("typecheckCOMPARE - type_idK epic fail\n");
			break;
		case intK:
			return 1;
			break;
		case boolK:
			return 1;
			break;
		case arrayK:
			return typecheckCOMPARE(type1->val.type, type2->val.type);
			break;
		case recordK:
			return typecheckCOMPARE_VAR_DECL_LIST(type1->val.var_decl_list,
					type2->val.var_decl_list);
			break;
		}

	} else {
		return 0;
	}

}

// Not tested yet
int typecheckCOMPARE_VAR_DECL_LIST(VAR_DECL_LIST *record1,
	VAR_DECL_LIST *record2) {
	VAR_TYPE *r1;
	VAR_TYPE *r2;
	TYPE *temp;
	if (record1->kind == record2->kind) {
		r1 = record1->var_type;
		r2 = record1->var_type;
		if (typecheckCOMPARE(r1->type, r2->type) == 1) {
			if (record1->kind == var_decl_list_baseK) {
				return 1;
			} else {
				return typecheckCOMPARE_VAR_DECL_LIST(record1->var_decl_list,
						record2->var_decl_list);
			}

		}
	} else {
		return 0;
	}
}

void typecheck_STATEMENTLIST(STATEMENT_LIST *statement_list,
		SymbolTable *symboltable) {
	switch(statement_list->kind){
	case statement_list_baseK:
		typecheck_STATEMENT(statement_list->statement,symboltable);
		break;
	case statement_listK:
		typecheck_STATEMENTLIST(statement_list->statement_list, symboltable);
		typecheck_STATEMENT(statement_list->statement,symboltable);
		break;
	}
}

void typecheck_STATEMENT(STATEMENT *statement,symboltable){
	EXPRESSION *expression;
	Symbol * symbol;

	TYPE *type;
	switch(statement->kind){
	case returnK:
		expression= statement->val.return_expression;
		break;
	case writeK:
		expression=statement->val.write_expression;
		break;
	case allocateK:

		break;
	case variableK:
		typecheckVARIABLE(statement->val.variableE.variable);

		if(typecheckCOMPARE()==1){

				}else{
					fail;
				}
		break;
	case ifK:
		if(statement->val.ifE.expression){

				}else{
					fail;
				}
		break;
	case whileK:
		if(statement->val.whileE.expression){

		}else{
			fail;
		}
		break;
	case statementlistK:
		typecheck_STATEMENTLIST(statement->val.statement_list, symboltable);
		break;
	}
}

Symbol *typecheckVAR(VARIABLE *variable,SymbolTable *symboltable){
	switch(variable->kind){
	case var_idK:
		return getSymbol(variable->val.id);
		break;
	case var_expK:

		break;
	case var_dotK:
		break;
	}
}

